using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Caching;
using ASPNETVN.PORTAL.Components.Configuration.Items;
using ASPNETVN.PORTAL.Components.Configuration.Settings;
using ASPNETVN.PORTAL.Components.UI;

namespace ASPNETVN.PORTAL.Components.Configuration
{
    public class PortalConfiguration
    {
        public void UpdatePortalInfo(int portalID, string portalName, string layoutName, string defaultTheme, bool alwaysShowEditButton, string language, bool allowRegister)
        {
            PortalDataSet ds = (PortalDataSet) HttpContext.Current.Items["PortalDataSet"];
            PortalDataSet.PortalRow globalRow = ds.Portal.FindByID(portalID);
            globalRow.ID = portalID;
            globalRow.Title = portalName;
            globalRow.Layout = layoutName;
            globalRow.DefaultTheme = defaultTheme;
            globalRow.AlwaysShowEditButton = alwaysShowEditButton;
            globalRow.Language = language;
            globalRow.AllowRegister = allowRegister;
            
            // Apply all theme of tabs in portal.
            foreach (PortalDataSet.TabRow tab in ds.Tab.Rows)
            {
                tab.ThemeName = globalRow.DefaultTheme;                
            }
            // Save settings.
            SaveSiteSettings();
        }

        //
        // TABS
        //

        //*********************************************************************
        //
        // AddTab Method <a name="AddTab"></a>
        //
        // The AddTab method adds a new tab to the portal.  These settings are 
        // stored in the Xml file PortalCfg.xml.
        //
        // Other relevant sources:
        //    + <a href="#SaveSiteSettings" style="color:green">SaveSiteSettings() method</a>
        //	  + <a href="PortalCfg.xml" style="color:green">PortalCfg.xml</a>
        //
        //*********************************************************************
        public int AddTab(int portalId, string tabName, int tabOrder)
        {
            // Obtain SiteSettings from Current Context
            PortalDataSet ds = (PortalDataSet) HttpContext.Current.Items["PortalDataSet"];

            // Create a new TabRow from the Tab table
            PortalDataSet.TabRow newRow = ds.Tab.NewTabRow();

            // Set the properties on the new row
            newRow.PortalID = Global.PortalID;
            newRow.Name = tabName;
            newRow.Order = tabOrder;
            // All users.
            newRow.AuthorizedRoles = "-1;";
            newRow.ShowLeftPane = true;
            newRow.ShowRightPane = true;
            newRow.ThemeName = "Blue";
            newRow.ShowOnMenu = true;

            // Add the new TabRow to the Tab table
            ds.Tab.AddTabRow(newRow);

            // Save the changes 
            SaveSiteSettings();

            // Return the new TabID
            return newRow.ID;
        }


        //*********************************************************************
        //
        // UpdateTab Method <a name="UpdateTab"></a>
        //
        // The UpdateTab method updates the settings for the specified tab. 
        // These settings are stored in the Xml file PortalCfg.xml.
        //
        // Other relevant sources:
        //    + <a href="#SaveSiteSettings" style="color:green">SaveSiteSettings() method</a>
        //	  + <a href="PortalCfg.xml" style="color:green">PortalCfg.xml</a>
        //
        //*********************************************************************
        public void UpdateTab(int portalId, int tabId, string tabName, int tabOrder, string authorizedRoles, string themeName, bool showLeftPane, bool showRightPane, bool showOnMenu)
        {
            // Obtain SiteSettings from Current Context
            PortalDataSet ds = (PortalDataSet) HttpContext.Current.Items["PortalDataSet"];

            // Find the appropriate tab in the Tab table and set the properties
            PortalDataSet.TabRow tabRow = ds.Tab.FindByID(tabId);
            tabRow.PortalID = portalId;
            tabRow.Name = tabName;
            tabRow.Order = tabOrder;
            tabRow.AuthorizedRoles = authorizedRoles;
            tabRow.ThemeName = themeName;
            tabRow.ShowLeftPane = showLeftPane;
            tabRow.ShowRightPane = showRightPane;
            tabRow.ShowOnMenu = showOnMenu;

            // Save the changes 
            SaveSiteSettings();
        }

        //*********************************************************************
        //
        // UpdateTabOrder Method <a name="UpdateTabOrder"></a>
        //
        // The UpdateTabOrder method changes the position of the tab with respect
        // to other tabs in the portal.  These settings are stored in the Xml 
        // file PortalCfg.xml.
        //
        // Other relevant sources:
        //    + <a href="#SaveSiteSettings" style="color:green">SaveSiteSettings() method</a>
        //	  + <a href="PortalCfg.xml" style="color:green">PortalCfg.xml</a>
        //
        //*********************************************************************
        public void UpdateTabOrder(int tabId, int tabOrder)
        {
            // Obtain SiteSettings from Current Context
            PortalDataSet ds = (PortalDataSet) HttpContext.Current.Items["PortalDataSet"];

            // Find the appropriate tab in the Tab table and set the property
            PortalDataSet.TabRow tabRow = ds.Tab.FindByID(tabId);

            tabRow.Order = tabOrder;

            // Save the changes 
            SaveSiteSettings();
        }

        public void SwapTabOrder(Tab t1, Tab t2)
        {
            int tmp = t1.Order;
            t1.Order = t2.Order;
            t2.Order = tmp;

            // Obtain SiteSettings from Current Context
            PortalDataSet ds = (PortalDataSet)HttpContext.Current.Items["PortalDataSet"];

            // Find the appropriate tab in the Tab table and set the property
            PortalDataSet.TabRow tabRow1 = ds.Tab.FindByID(t1.ID);
            tabRow1.Order = t1.Order;
            PortalDataSet.TabRow tabRow2 = ds.Tab.FindByID(t2.ID);
            tabRow2.Order = t2.Order;
            // Save the changes 
            SaveSiteSettings();
        }

        //-----------------------------------------------------------------------------------------
        public void DeleteTab(int tabID)
        {
            PortalDataSet settings = (PortalDataSet) HttpContext.Current.Items["PortalDataSet"];

            //
            // Delete information in the Database relating to each Module being deleted
            //

            settings.Tab.RemoveTabRow(settings.Tab.FindByID(tabID));
            SaveSiteSettings();
        }

        //-----------------------------------------------------------------------------------------
        public void UpdateModuleOrder(int ModuleId, int ModuleOrder, string pane)
        {
            // Obtain SiteSettings from Current Context
            PortalDataSet ds = (PortalDataSet) HttpContext.Current.Items["PortalDataSet"];

            // Find the appropriate Module in the Module table and update the properties
            PortalDataSet.ModuleRow moduleRow = ds.Module.FindByID(ModuleId);

            moduleRow.Order = ModuleOrder;
            moduleRow.PaneName = pane;

            // Save the changes 
            SaveSiteSettings();
        }


        //-----------------------------------------------------------------------------------------
        public int AddModule(int tabId, int moduleOrder, string paneName, bool showEveryWhere, string title, int moduleDefId, int cacheTime, string viewRoles, string addRoles, string editRoles, string deleteRoles, string settingRoles, string managerRoles, string securityRoles)
        {
            // Obtain SiteSettings from Current Context
            PortalDataSet ds = (PortalDataSet) HttpContext.Current.Items["PortalDataSet"];

            // Create a new ModuleRow from the Module table
            PortalDataSet.ModuleRow newModule = ds.Module.NewModuleRow();

            // Set the properties on the new Module
            newModule.ModuleDefinitionID = moduleDefId;
            newModule.Order = moduleOrder;
            newModule.ShowEveryWhere = showEveryWhere;
            newModule.Title = title;
            newModule.PaneName = paneName;

            newModule.AuthorizedViewRoles = viewRoles;
            newModule.AuthorizedAddRoles = addRoles;
            newModule.AuthorizedEditRoles = editRoles;
            newModule.AuthorizedDeleteRoles = deleteRoles;
            newModule.AuthorizedSettingRoles = settingRoles;
            newModule.AuthorizedManagerRoles = managerRoles;
            newModule.AuthorizedSecurityRoles = securityRoles;

            newModule.CacheTimeout = cacheTime;
            newModule.TabRow = ds.Tab.FindByID(tabId);

            // Add the new ModuleRow to the Module table
            ds.Module.AddModuleRow(newModule);

            // Save the changes
            SaveSiteSettings();

            // Return the new Module ID
            return newModule.ID;
        }


        //-----------------------------------------------------------------------------------------
        public int UpdateModule(int moduleId, int moduleOrder, string paneName, bool showEveryWhere, string title, int cacheTime, string viewRoles, string addRoles, string editRoles, string deleteRoles, string settingRoles, string managerRoles, string securityRoles)
        {
            // Obtain SiteSettings from Current Context
            PortalDataSet ds = (PortalDataSet) HttpContext.Current.Items["PortalDataSet"];

            // Find the appropriate Module in the Module table and update the properties
            PortalDataSet.ModuleRow moduleRow = ds.Module.FindByID(moduleId);

            moduleRow.Order = moduleOrder;
            moduleRow.ShowEveryWhere = showEveryWhere;
            moduleRow.Title = title;
            moduleRow.PaneName = paneName;
            moduleRow.CacheTimeout = cacheTime;

            moduleRow.AuthorizedViewRoles = viewRoles;
            moduleRow.AuthorizedAddRoles = addRoles;
            moduleRow.AuthorizedEditRoles = editRoles;
            moduleRow.AuthorizedDeleteRoles = deleteRoles;
            moduleRow.AuthorizedSettingRoles = settingRoles;
            moduleRow.AuthorizedManagerRoles = managerRoles;
            moduleRow.AuthorizedSecurityRoles = securityRoles;

            // Save the changes 
            SaveSiteSettings();

            // Return the existing Module ID
            return moduleId;
        }

        public int UpdateModule(int moduleId, string viewRoles, string addRoles, string editRoles, string deleteRoles, string settingRoles, string managerRoles, string securityRoles)
        {
            // Obtain SiteSettings from Current Context
            PortalDataSet ds = (PortalDataSet)HttpContext.Current.Items["PortalDataSet"];

            // Find the appropriate Module in the Module table and update the properties
            PortalDataSet.ModuleRow moduleRow = ds.Module.FindByID(moduleId);

            moduleRow.AuthorizedViewRoles = viewRoles;
            moduleRow.AuthorizedAddRoles = addRoles;
            moduleRow.AuthorizedEditRoles = editRoles;
            moduleRow.AuthorizedDeleteRoles = deleteRoles;
            moduleRow.AuthorizedSettingRoles = settingRoles;
            moduleRow.AuthorizedManagerRoles = managerRoles;
            moduleRow.AuthorizedSecurityRoles = securityRoles;

            // Save the changes 
            SaveSiteSettings();

            // Return the existing Module ID
            return moduleId;
        }

        public int UpdateModule(int moduleId, int moduleOrder, string paneName, bool showEveryWhere, string title, int cacheTime)
        {
            // Obtain SiteSettings from Current Context
            PortalDataSet ds = (PortalDataSet)HttpContext.Current.Items["PortalDataSet"];

            // Find the appropriate Module in the Module table and update the properties
            PortalDataSet.ModuleRow moduleRow = ds.Module.FindByID(moduleId);

            moduleRow.Order = moduleOrder;
            moduleRow.ShowEveryWhere = showEveryWhere;
            moduleRow.Title = title;
            moduleRow.PaneName = paneName;
            moduleRow.CacheTimeout = cacheTime;

            // Save the changes 
            SaveSiteSettings();

            // Return the existing Module ID
            return moduleId;
        }

        //-----------------------------------------------------------------------------------------
        public void DeleteModule(int moduleID)
        {
            PortalDataSet settings = (PortalDataSet) HttpContext.Current.Items["PortalDataSet"];

            //
            // Delete information in the Database relating to Module being deleted
            //            

            settings.Module.RemoveModuleRow(settings.Module.FindByID(moduleID));
            SaveSiteSettings();
        }


        
        public static void UpdateModuleSettings(int moduleID, ModuleSettingItem[] settingItems)
        {
            // Obtain SiteSettings from Current Context
            PortalDataSet ds = (PortalDataSet)HttpContext.Current.Items["PortalDataSet"];
            // if (ds == null) HttpContext.Current.Items["PortalDataSet"] = GetPortalDataSet();
            // Find the appropriate Module in the Module table
            PortalDataSet.ModuleRow moduleRow = ds.Module.FindByID(moduleID);

            PortalDataSet.ModuleSettingRow[] settingRows = moduleRow.GetModuleSettingRows();
            PortalDataSet.ModuleSettingRow settingRow;
            foreach (ModuleSettingItem item in settingItems)
            {
                if (settingRows.Length == 0)
                {
                    // If there are no Setting elements at all, add one with the new name and value,
                    // but only if the value is not empty
                    if (item.Value != string.Empty)
                    {
                        settingRow = ds.ModuleSetting.NewModuleSettingRow();
                        settingRow.ModuleID = moduleRow.ID;
                        settingRow.Key = item.EditControl.ID;
                        settingRow.Value = item.Value;

                        ds.ModuleSetting.AddModuleSettingRow(settingRow);
                    }
                }
                else
                {
                    // Update existing setting element if it matches
                    bool found = false;
                    int i;

                    // Find which row matches the input parameter "key" and update the
                    // value.  If the value is string.Empty, however, delete the row.
                    for (i = 0; i < settingRows.Length; i++)
                    {
                        if (settingRows[i].Key == item.EditControl.ID)
                        {
                            if (item.Value == string.Empty)
                            {
                                // Delete the row
                                ds.ModuleSetting.RemoveModuleSettingRow(settingRows[i]);
                            }
                            else
                            {
                                // Update the value
                                settingRows[i].Value = item.Value;
                            }

                            found = true;
                            break;
                        }
                    }

                    if (found == false)
                    {
                        // Setting elements exist, however, there is no matching Setting element.
                        // Add one with new name and value, but only if the value is not empty
                        if (item.Value != string.Empty)
                        {
                            settingRow = ds.ModuleSetting.NewModuleSettingRow();
                            settingRow.ModuleID = moduleRow.ID;
                            settingRow.Key = item.EditControl.ID;
                            settingRow.Value = item.Value;

                            ds.ModuleSetting.AddModuleSettingRow(settingRow);
                        }
                    }
                }                
            }
            
            

            // Save the changes.
            SaveSiteSettings();            
        }

        //*********************************************************************
        //
        // UpdateModuleSetting Method  <a name="UpdateModuleSetting"></a>
        //
        // The UpdateModuleSetting Method updates a single module setting 
        // in the configuration file.  If the value passed in is string.Empty,
        // the Setting element is deleted if it exists.  If not, either a 
        // matching Setting element is updated, or a new Setting element is 
        // created.
        //
        // Other relevant sources:
        //    + <a href="#SaveSiteSettings" style="color:green">SaveSiteSettings() method</a>
        //	  + <a href="PortalCfg.xml" style="color:green">PortalCfg.xml</a>
        //
        //*********************************************************************
        public static void UpdateModuleSetting(int moduleID, object key, string value)
        {
            // Obtain SiteSettings from Current Context
            PortalDataSet ds = (PortalDataSet) HttpContext.Current.Items["PortalDataSet"];
            // if (ds == null) HttpContext.Current.Items["PortalDataSet"] = GetPortalDataSet();
            // Find the appropriate Module in the Module table
            PortalDataSet.ModuleRow moduleRow = ds.Module.FindByID(moduleID);

            PortalDataSet.ModuleSettingRow[] settingRows = moduleRow.GetModuleSettingRows();
            PortalDataSet.ModuleSettingRow settingRow;
            if (settingRows.Length == 0)
            {
                // If there are no Setting elements at all, add one with the new name and value,
                // but only if the value is not empty
                if (value != string.Empty)
                {
                    settingRow = ds.ModuleSetting.NewModuleSettingRow();
                    settingRow.ModuleID = moduleRow.ID;
                    settingRow.Key = key.ToString();
                    settingRow.Value = value;                    

                    ds.ModuleSetting.AddModuleSettingRow(settingRow);
                }
            }
            else
            {
                // Update existing setting element if it matches
                bool found = false;
                int i;

                // Find which row matches the input parameter "key" and update the
                // value.  If the value is string.Empty, however, delete the row.
                for (i = 0; i < settingRows.Length; i++)
                {
                    if (settingRows[i].Key == key.ToString())
                    {
                        if (value == string.Empty)
                        {
                            // Delete the row
                            ds.ModuleSetting.RemoveModuleSettingRow(settingRows[i]);
                        }
                        else
                        {
                            // Update the value
                            settingRows[i].Value = value;
                        }

                        found = true;
                        break;
                    }
                }

                if (found == false)
                {
                    // Setting elements exist, however, there is no matching Setting element.
                    // Add one with new name and value, but only if the value is not empty
                    if (value != string.Empty)
                    {
                        settingRow = ds.ModuleSetting.NewModuleSettingRow();
                        settingRow.ModuleID = moduleRow.ID;
                        settingRow.Key = key.ToString();
                        settingRow.Value = value;

                        ds.ModuleSetting.AddModuleSettingRow(settingRow);
                    }
                }
            }

            // Save the changes.
            SaveSiteSettings();
        }

        //*********************************************************************
        //
        // GetModuleSettings Method  <a name="GetModuleSettings"></a>
        //
        // The GetModuleSettings Method returns a hashtable of custom,
        // module-specific settings from the configuration file.  This method is
        // used by some user control modules (Xml, Image, etc) to access misc
        // settings.
        //
        // Other relevant sources:
        //    + <a href="#SaveSiteSettings" style="color:green">SaveSiteSettings() method</a>
        //	  + <a href="PortalCfg.xml" style="color:green">PortalCfg.xml</a>
        //
        //*********************************************************************
        

        
        public static Hashtable GetModuleSettings(int moduleID)
        {
            // Create a new Hashtable
            Hashtable _settingsHT = new Hashtable();

            // Obtain SiteSettings from Current Context
            PortalDataSet ds = (PortalDataSet) HttpContext.Current.Items["PortalDataSet"];

            // Find the appropriate Module in the Module table
            PortalDataSet.ModuleRow moduleRow = ds.Module.FindByID(moduleID);

            foreach (PortalDataSet.ModuleSettingRow sRow in moduleRow.GetModuleSettingRows())
            {
                _settingsHT[sRow.Key] = sRow.Value;
            }

            return _settingsHT;
        }


        public static Hashtable GetModuleSettings(int moduleID, PortalModuleControl control)
        {
            Hashtable setting = GetModuleSettings(moduleID, control.BaseSettings);
            return setting;
        }

        public static Hashtable GetModuleSettings(int moduleID, System.Web.UI.Page page)
        {
            // Obtain SiteSettings from Current Context
            PortalDataSet ds = (PortalDataSet)HttpContext.Current.Items["PortalDataSet"];

            // Find the appropriate Module in the Module table
            PortalDataSet.ModuleRow moduleRow = ds.Module.FindByID(moduleID);

            PortalDataSet.ModuleDefinitionRow moduledefineRow = ds.ModuleDefinition.FindByID(moduleRow.ModuleDefinitionID);

            PortalModuleControl portalModule = (PortalModuleControl)page.LoadControl(moduledefineRow.SourceFile);
            Hashtable setting = GetModuleSettings(moduleID, portalModule.BaseSettings);
            return setting;
        }


        public static Hashtable GetModuleSettings(int moduleID, Hashtable baseSettings)
        {
            // Create a new Hashtable
            Hashtable _settings = new Hashtable();

            // Obtain SiteSettings from Current Context
            PortalDataSet ds = (PortalDataSet)HttpContext.Current.Items["PortalDataSet"];

            // Find the appropriate Module in the Module table
            PortalDataSet.ModuleRow moduleRow = ds.Module.FindByID(moduleID);

            foreach (PortalDataSet.ModuleSettingRow sRow in moduleRow.GetModuleSettingRows())
            {
                _settings[sRow.Key] = sRow.Value;
            }

            foreach (string key in baseSettings.Keys)
            {
                if (_settings[key] != null)
                {
                    ModuleSettingItem s = ((ModuleSettingItem)baseSettings[key]);
                    if (_settings[key].ToString() != string.Empty)
                        s.Value = _settings[key].ToString();
                }
            }
            return baseSettings;
        }


        //
        // MODULE DEFINITIONS
        //

        //*********************************************************************
        //
        // GetModuleDefinitions() Method <a name="GetModuleDefinitions"></a>
        //
        // The GetModuleDefinitions method returns a list of all module type 
        // definitions for the portal.
        //
        // Other relevant sources:
        //    + <a href="#SaveSiteSettings" style="color:green">SaveSiteSettings() method</a>
        //	  + <a href="PortalCfg.xml" style="color:green">PortalCfg.xml</a>
        //
        //*********************************************************************
        public DataRow[] GetModuleDefinitions(int portalId)
        {
            // Obtain SiteSettings from Current Context
            PortalDataSet ds = (PortalDataSet) HttpContext.Current.Items["PortalDataSet"];

            // Find the appropriate Module in the Module table
            return ds.ModuleDefinition.Select();
        }

        //*********************************************************************
        //
        // AddModuleDefinition() Method <a name="AddModuleDefinition"></a>
        //
        // The AddModuleDefinition add the definition for a new module type
        // to the portal.
        //
        // Other relevant sources:
        //    + <a href="#SaveSiteSettings" style="color:green">SaveSiteSettings() method</a>
        //	  + <a href="PortalCfg.xml" style="color:green">PortalCfg.xml</a>
        //
        //*********************************************************************
        public int AddModuleDefinition(int portalId, string name, string desktopSrc, Guid guid)
        {
            // Obtain SiteSettings from Current Context
            PortalDataSet ds = (PortalDataSet) HttpContext.Current.Items["PortalDataSet"];

            // Create new ModuleDefinitionRow
            PortalDataSet.ModuleDefinitionRow newModuleDef = ds.ModuleDefinition.NewModuleDefinitionRow();

            // Set the parameter values
            newModuleDef.Name = name;
            newModuleDef.SourceFile = desktopSrc;
            newModuleDef.GeneralDefinitionID = guid;

            // Add the new ModuleDefinitionRow to the ModuleDefinition table
            ds.ModuleDefinition.AddModuleDefinitionRow(newModuleDef);

            // Save the changes
            SaveSiteSettings();

            // Return the new ModuleDefID
            return newModuleDef.ID;
        }

        //*********************************************************************
        //
        // DeleteModuleDefinition() Method <a name="DeleteModuleDefinition"></a>
        //
        // The DeleteModuleDefinition method deletes the specified module type 
        // definition from the portal.  Each module which is related to the
        // ModuleDefinition is deleted from each tab in the configuration
        // file, and all data relating to each module is deleted from the
        // database.
        //
        // Other relevant sources:
        //    + <a href="#SaveSiteSettings" style="color:green">SaveSiteSettings() method</a>
        //	  + <a href="PortalCfg.xml" style="color:green">PortalCfg.xml</a>
        //    + <a href="DeleteModule.htm" style="color:green">DeleteModule Stored Procedure</a>
        //
        //*********************************************************************
        public void DeleteModuleDefinition(int defId)
        {
            // Obtain SiteSettings from Current Context
            PortalDataSet ds = (PortalDataSet) HttpContext.Current.Items["PortalDataSet"];

            //
            // Delete information in the Database relating to each Module being deleted
            //

            // Finish removing Module Definition
            ds.ModuleDefinition.RemoveModuleDefinitionRow(ds.ModuleDefinition.FindByID(defId));

            // Save the changes 
            SaveSiteSettings();
        }

        //*********************************************************************
        //
        // UpdateModuleDefinition() Method <a name="UpdateModuleDefinition"></a>
        //
        // The UpdateModuleDefinition method updates the settings for the 
        // specified module type definition.
        //
        // Other relevant sources:
        //    + <a href="#SaveSiteSettings" style="color:green">SaveSiteSettings() method</a>
        //	  + <a href="PortalCfg.xml" style="color:green">PortalCfg.xml</a>
        //
        //*********************************************************************
        public void UpdateModuleDefinition(int defId, string name, string desktopSrc, Guid guid)
        {
            // Obtain SiteSettings from Current Context
            PortalDataSet ds = (PortalDataSet) HttpContext.Current.Items["PortalDataSet"];

            // Find the appropriate Module in the Module table and update the properties
            PortalDataSet.ModuleDefinitionRow modDefRow = ds.ModuleDefinition.FindByID(defId);

            modDefRow.Name = name;
            modDefRow.SourceFile = desktopSrc;
            modDefRow.GeneralDefinitionID = guid;

            // Save the changes 
            SaveSiteSettings();
        }

        //*********************************************************************
        //
        // GetSingleModuleDefinition Method
        //
        // The GetSingleModuleDefinition method returns a ModuleDefinitionRow
        // object containing details about a specific module definition in the
        // configuration file.
        //
        // Other relevant sources:
        //    + <a href="#SaveSiteSettings" style="color:green">SaveSiteSettings() method</a>
        //	  + <a href="PortalCfg.xml" style="color:green">PortalCfg.xml</a>
        //
        //*********************************************************************
        public PortalDataSet.ModuleDefinitionRow GetSingleModuleDefinition(int defId)
        {
            // Obtain SiteSettings from Current Context
            PortalDataSet ds = (PortalDataSet)HttpContext.Current.Items["PortalDataSet"];

            // Find the appropriate Module in the Module table
            return ds.ModuleDefinition.FindByID(defId);
        }

        //*********************************************************************
        //
        // GetSiteSettings Static Method
        //
        // The Configuration.GetSiteSettings Method returns a typed
        // dataset of the all of the site configuration settings from the
        // XML configuration file.  This method is used in Global.asax to
        // push the settings into the current HttpContext, so that all of the 
        // pages, content modules and classes throughout the rest of the request
        // may access them.
        //
        // The PortalDataSet object is cached using the ASP.NET Cache API,
        // with a file-change dependency on the XML configuration file.  Normallly,
        // this method just returns a copy of the object in the cache.  When the
        // configuration is updated and changes are saved to the the XML file,
        // the PortalDataSet object is evicted from the cache.  The next time 
        // this method runs, it will read from the XML file again and insert a
        // fresh copy of the PortalDataSet into the cache.
        //
        //*********************************************************************
        public static PortalDataSet GetPortalDataSet()
        {
            PortalDataSet ds = (PortalDataSet) HttpContext.Current.Cache["PortalDataSet"];

            // If the PortalDataSet isn't cached, load it from the XML file and add it into the cache.
            if (ds == null)
            {
                // Create the dataset
                ds = new PortalDataSet();

                // Retrieve the location of the XML configuration file
                string configFile = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["configFile"]);

                // Set the AutoIncrement property to true for easier adding of rows
                ds.Tab.IDColumn.AutoIncrement = true;
                ds.Module.IDColumn.AutoIncrement = true;
                ds.ModuleDefinition.IDColumn.AutoIncrement = true;

                // Load the XML data into the DataSet
                ds.ReadXml(configFile);

                // Store the dataset in the cache
                HttpContext.Current.Cache.Insert("PortalDataSet", ds, new CacheDependency(configFile));
            }

            return ds;
        }

        //*********************************************************************
        //
        // SaveSiteSettings Method <a name="SaveSiteSettings"></a>
        //
        // The Configuration.SaveSiteSettings overwrites the the XML file with the
        // settings in the PortalDataSet object in context.  The object will in 
        // turn be evicted from the cache and be reloaded from the XML file the next
        // time GetSiteSettings() is called.
        //
        //*********************************************************************
        private static void SaveSiteSettings()
        {
            PortalDataSet ds = (PortalDataSet) HttpContext.Current.Cache["PortalDataSet"];
            if (ds == null)
            {
                ds = GetPortalDataSet();
            }
            string configFile = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["configFile"]);
            ds.WriteXml(configFile);
        }
    }
}